package ws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Symbols {


    private static final String dev = "http://api2.cht-test-1.bybit.com/option/usdc/public/optionmp/query/symbols";
    private static final String sit = "http://api2.usdc-test-1.bybit.com/option/usdc/public/optionmp/query/symbols";
    private static final String testnet = "https://api2-testnet.bybit.com/option/usdc/public/optionmp/query/symbols";

    public static List<JSONObject> symbols(String env) throws IOException {
        String url;

        switch (env) {
            case "dev" -> url = dev;
            case "sit" -> url = sit;
            case "testnet" -> url = testnet;
            default -> throw new IllegalStateException("Unexpected value: " + env);
        }

        OkHttpClient mClient = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(30, TimeUnit.SECONDS)//设置写的超时时间
                .connectTimeout(30, TimeUnit.SECONDS)//设置连接超时时间
                .build();

        //构建一个连接请求对象
        RequestBody body = new FormBody.Builder()
                .add("baseCoin", "BTC")
                .add("standard", "U")
                .build();
        Request request = new Request.Builder()
                .post(body)
                .url(url)
                .build();

        Call call = mClient.newCall(request);

        Response response = call.execute();

        String jsonStr = response.body().string();

        Map<String, Object> map = JSON.parseObject(jsonStr, Map.class);

        Map<String, List<JSONObject>> result = (Map) map.get("result");

        return result.get("symbolList");
    }

    public static void main(String[] args) throws IOException {
        for (JSONObject symbol : Symbols.symbols("dev")) {
            System.out.println(symbol.get("symbolName"));
        }
    }
}
